package com.black.cat.jvm.rtda.heap.methodarea;

import com.black.cat.jvm.classfile.MemberInfo;
import com.black.cat.jvm.classfile.attribute.ConstantValueAttribute;

/**
 * @Author blackcat
 * @create 2021/8/14 10:46
 * @version: 1.0
 * @description:字段类
 */
public class JField extends ClassMember {

    public int constValueIndex;
    public int slotId;

    public JField[] newFields(JClass clazz, MemberInfo[] cfFields) {
        JField[] JFields = new JField[cfFields.length];
        for (int i = 0; i < cfFields.length; i++) {
            JFields[i] = new JField();
            JFields[i].clazz = clazz;
            JFields[i].copyMemberInfo(cfFields[i]);
            JFields[i].copyAttributes(cfFields[i]);
        }
        return JFields;
    }

    public void copyAttributes(MemberInfo cfField) {
        ConstantValueAttribute valAttr = cfField.ConstantValueAttribute();
        if (null != valAttr) {
            this.constValueIndex = valAttr.constantValueIdx();
        }
    }

    public int constValueIndex() {
        return this.constValueIndex;
    }

    public int slotId() {
        return this.slotId;
    }

    public boolean isLongOrDouble() {
        return this.descriptor.equals("J") || this.descriptor.equals("D");
    }
}
